<?php
$edit_img = Yii::app()->baseUrl . '/images/icons/edit-26.png';
$edit_btn = '<img src="'.$edit_img.'" rel="tipsy" title="'.BUtils::T('special_edit_eg').'" />';
$delete_img = Yii::app()->baseUrl . '/images/icons/delete.png';
$delete_btn = '<img src="'.$delete_img.'" rel="tipsy" title="'.BUtils::T('special_delete_eg').'" />';
$publish_img = Yii::app()->baseUrl . '/images/icons/paper_plane-26.png';
$publish_btn = '<img src="'.$publish_img.'" rel="tipsy" title="'.BUtils::T('special_publish_eg').'" />';
$new_offer_img = Yii::app()->baseUrl . '/images/icons/add_file-26.png';
$new_offer_btn = '<img src="'.$new_offer_img.'" rel="tipsy" title="'.BUtils::T('special_add_eg').'" />';
//share to facebook: only for published offer
$fb_share_img = Yii::app()->baseUrl . '/images/icons/facebook-26.png';
$fb_share_btn = '<img src="'.$fb_share_img.'" rel="tipsy" title="'.BUtils::T('special_fb_share_eg').'" />';
//pop up a report for this special.
$view_report_img = Yii::app()->baseUrl . '/images/icons/combo-26.png';
$view_report_btn = '<img src="'.$view_report_img.'" rel="tipsy" title="'.BUtils::T('special_report_eg').'" />';
$view_report_btn_disabled = '<img src="'.$view_report_img.'" rel="tipsy" title="'.BUtils::T('special_report_not_ready_eg').'" />';


$reach_lbl = BUtils::T('special_report_reach_lbl');
$reach_eg = BUtils::T('special_report_reach_eg');
$clicks_lbl = BUtils::T('special_report_clicks_lbl');
$clicks_eg = BUtils::T('special_report_clicks_eg');
$notifications_lbl = BUtils::T('special_report_notifications_lbl');
$notifications_eg = BUtils::T('special_report_notifications_eg');
$spread_lbl = BUtils::T('special_report_spread_lbl');
$spread_eg = BUtils::T('special_report_spread_eg');
$redeem_lbl = BUtils::T('special_report_redeems_lbl');
$redeem_eg = BUtils::T('special_report_redeems_eg');

$unpublished_lbl = BUtils::T('unpublished_lbl');
$special_active_lbl = BUtils::T('special_active_lbl');
$special_pending_lbl = BUtils::T('special_pending_lbl');
$special_completed_lbl = BUtils::T('special_completed_lbl');

$predefined_days = BUtils::WeekDayKeys(7, true);

echo '<div style="text-align:right; padding:10px; border-bottom:1px solid #eee;">';
$this->widget('CLinkPager', array(
	'currentPage' => $pages->getCurrentPage(),
	'itemCount' => $pages->getItemCount(),
	'pageSize' => $pages->getPageSize(),
	'prevPageLabel' => '&lt;',
	'nextPageLabel' => '&gt;',
	'firstPageLabel' => '&lt;&lt;',
	'lastPageLabel' => '&gt;&gt;',
	'maxButtonCount' => 5,
	'header' => '',
	'htmlOptions' => array('class'=>'pagination'),
));
echo '</div>';
foreach ($model as $k => $r) {
	//set offer picture
	$url_thumb_default = Yii::app()->baseUrl . '/images/default_special_thumb.png';
	$url_picture_default = Yii::app()->baseUrl . '/images/default_special_picture.png';
	$url_thumb = empty($r['url_picture']) ? $r['url_offer_img'] : $r['url_picture'];
	if (!empty($url_thumb)) {
		$url_thumb = $url_thumb;
		$url_picture = str_replace('thumb_', '', $url_thumb);
	} else {
		$url_thumb = $url_thumb_default;
		$url_picture = $url_picture_default;
	}
	
	//offer valid time
	$range_time = BUtils::GetHourRangeFormat($r['start_hour'], $r['end_hour']);
	//offer valid date
	$range_date = BUtils::GetDateRangeString($r['uts_start_date'], $r['uts_end_date']);
	//offer valid date
	$range_week = BUtils::GetWeekdays($r['week_days'], $predefined_days);
	
	//set price, discount or highlight
	$highlight = $del_price = '';
	if (!empty($r['price'])) {
		$highlight = MerchantObject::formatPrice($r['price']);
		if (!empty($r['value'])) {
			$del_price = '<span class="oldprice label">€'.$r['value'].'</span>';
		}
	} elseif (!empty($r['discount'])) {
		$highlight = MerchantObject::formatDiscount($r['discount']);
	}
	
	//offer content
	$content = strip_tags(trim($r['content']));
	$content = strlen($content) > 72 ? substr($content, 0, 72) . '...' : $content;
	
	//set expired or upcoming offer style
	$now = time();
	if ($r['status'] == 0) {
		if ($now > $r['uts_end_date']) $style="background:#ff8888;";
		else $style = '';
		$icon = '<i class="icon icon-time"></i>';
		$flag = '<span class="status" style="'.$style.'">'.$unpublished_lbl.'</span>';
	} elseif ($now < $r['uts_start_date']) {
		$icon = '<i class="icon icon-time"></i>';
		$flag = '<span class="status upcoming">'.$special_pending_lbl.'</span>';
	} elseif ($now > $r['uts_end_date']) {
		$icon = '<i class="icon icon-ok"></i>';
		$flag = '<span class="status expired">'.$special_completed_lbl.'</span>';
	} else {
		$icon = '<i class="icon icon-play"></i>';
		$flag = '<span class="status current">'.$special_active_lbl.'</span>';
	}
	
	if ($now < $r['uts_start_date']) {
		$ex_days = 0;//pending status
	} elseif ($now > $r['uts_end_date']) {
		$ex_days = round(($r['uts_end_date'] - $r['uts_start_date']) / 3600 / 24);
	} else {
		$ex_days = round((time() - $r['uts_start_date']) / 3600 / 24);
	}
	
	if ($ex_days === 0) {
		//pending status
		$reach = $clicks = $notifications = $spread = $redeems = '-';
	} else {
		$clicks = round($r['clicks']);
		$notifications = round($r['notifications']);
		$spread = round($r['spread']);
		$redeems = round($r['redeems']);
		$reach = round($r['reach']);
		if ($reach < 1) {
			$reach = $clicks = $notifications = $spread = $redeems = '-';
		}
	}
	
	$fb_like_button = '';
	if ($now < $r['uts_end_date'] and $r['status'] == 1) {
		$fb_like_button = '<a data-url="http://yamii.fi/special/'.$r['id'].'" class="button icon-btn fb-like-callback">' . $fb_share_btn . '</a>';
	}
?>
<div class="mb_offer" style="padding:8px 0; border-bottom:1px solid #ddd;">
	<div class="mb_picture">
		<a href="<?php echo Yii::app()->createUrl('/special/preview', array('id'=>$r['id'])) ?>" class="popup-btn">
			<img src="<?php echo $url_thumb; ?>" alt="" onError="this.onerror=null;this.src='<?php echo $url_thumb_default; ?>';" />
		</a>
	</div>
	<div class="mb_price" style="width:100px;">
		<div class="highlight"><?php echo $highlight; ?></div>
		<?php echo $del_price; ?>
	</div>
	<div class="mb_period">
		<div class="time"><?php echo $range_time; ?></div>
		<div class="date"><?php echo $range_week; ?></div>
	</div>
	<div class="mb_text" style="margin:0;">
		<a href="<?php echo Yii::app()->createUrl('/special/preview', array('id'=>$r['id'])) ?>" class="popup-btn"><?php echo $content; ?></a>
	</div>
	<div class="clear"></div>
	<div class="table_title"></div>
	<table style="margin-left:4px; margin-top:2px;" border="1">
		<tr>
			<td class="center"><?php echo $reach_lbl; ?><i class="icon icon-question-sign pointer" rel="tipsy" title="<?php echo $reach_eg; ?>"></i></td>
			<td class="center"><?php echo $clicks_lbl; ?><i class="icon icon-question-sign pointer" rel="tipsy" title="<?php echo $clicks_eg; ?>"></i></td>
			<td class="center"><?php echo $notifications_lbl; ?><i class="icon icon-question-sign pointer" rel="tipsy" title="<?php echo $notifications_eg; ?>"></i></td>
			<!-- <td class="center"><?php echo $spread_lbl; ?><i class="icon icon-question-sign pointer" rel="tipsy" title="<?php echo $spread_eg; ?>"></i></td> -->
			<td class="center"><?php echo $redeem_lbl; ?><i class="icon icon-question-sign pointer" rel="tipsy" title="<?php echo $redeem_eg; ?>"></i></td>
			<td class="action-field"><span class=""><?php echo $range_date; ?></span></td>
		</tr>
		<tr>
			<td class="center"><?php echo $reach; ?></td>
			<td class="center"><?php echo $clicks; ?></td>
			<td class="center"><?php echo $notifications; ?></td>
			<!-- <td class="center"><?php echo $spread; ?></td> -->
			<td class="center"><?php echo $redeems; ?></td>
			<td style="text-align:right; padding:0;">
<?php
if (empty($r['status'])) {
	echo CHtml::link($edit_btn, Yii::app()->createUrl('/special/form', array('id'=>$r['id'])), array('class'=>'button icon-btn unpub'));
	echo CHtml::link($publish_btn, Yii::app()->createUrl('/special/publish', array('id'=>$r['id'])), array('class'=>'button button-compliment icon-btn publish-btn unpub'));
	if ($reach == '-') echo CHtml::link($view_report_btn_disabled, 'javascript:;', array('class'=>'button button-disabled icon-btn'));
	else echo CHtml::link($view_report_btn, Yii::app()->createUrl('/special/reportDaily', array('id'=>$r['id'])), array('class'=>'button button-compliment icon-btn popup-btn'));
} elseif ($r['status'] == 1) {
	echo $fb_like_button;
	if ($reach == '-') echo CHtml::link($view_report_btn_disabled, 'javascript:;', array('class'=>'button button-disabled icon-btn'));
	else echo CHtml::link($view_report_btn, Yii::app()->createUrl('/special/reportDaily', array('id'=>$r['id'])), array('class'=>'button button-compliment icon-btn popup-btn'));
	echo CHtml::link($new_offer_btn, Yii::app()->createUrl('/special/form', array('sid'=>$r['id'])), array('class'=>'button icon-btn'));
}
echo CHtml::link($delete_btn, Yii::app()->createUrl('/special/delete', array('id'=>$r['id'])), array('class'=>'button icon-btn delete-btn unpub'));
?>
			</td>
		</tr>
	</table>
</div>
<?php
}
?>
<script type="text/javascript">
$(function() {
	$('.pagination a').click(function(){
		$.ajax({
			'beforeSend':beforeSend, 'success':success,
			'type':'POST', 'url':$(this).attr('href'), 'data':$('#search_form').serialize()
		});
		return false;
	});
	
	$('.popup-btn').facebox({
		loadingImage: '<?php echo Yii::app()->baseUrl ?>/images/loader-bar.gif',
		closeImage: '<?php echo Yii::app()->baseUrl ?>/images/btn-close.png',
		overlayClose:true,
		width: 360
	});
	$('.delete-btn').click(function() {
		if (confirm('<?php echo BUtils::T('delete_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().parent().parent().parent().parent().fadeOut();
				}
			});
		}
		return false;
	});
	$('.publish-btn').click(function() {
		if (confirm('<?php echo BUtils::T('publish_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().children('.unpub').hide();
				}
			});
		}
		return false;
	});
	$('.fb-like-callback').click(function(){
		fbUI.share($(this).data('url'));
	});
});
</script>